package com.briup.day22;

/**
 * @author ThinkBook
 */
public class Test08_Runnable {
    public static void main(String[] args) {
        //实例化线程对象
        Runnable r = new MyRunnable();
        Thread th1 = new Thread(r);

        //设置线程名称
        th1.setName("子线程1");

        //启动子线程
        th1.start();

        System.out.println("------------");

        //可以使用匿名内部类对象
        Runnable r2 = new Runnable() {
            @Override
            public void run() {
                String name = Thread.currentThread().getName();
                System.out.println("in " + name + " run ...");
            }
        };
        Thread th2 = new Thread(r2);
        th2.start();
    }
}

class MyRunnable implements Runnable {
    // 等同 线程处理方法
    @Override
    public void run() {
        String name = Thread.currentThread().getName();
//        String name = this.getName(); ERROR
        for (int i = 70; i >= 20; i--) {
            System.out.println("in " + name + " run, i: " + i);
        }
    }

}
